﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BrainTechLLC.WebCamLib3;
using System.Threading;

namespace WpfImageBroadcaster
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        protected CameraHandler _cameraHandler;
        protected Broadcaster _broadcaster;
        protected Communications _server;

        public Window1()
        {
            InitializeComponent();
            _cameraHandler = new CameraHandler();
            cameraControl.cameraHandler = _cameraHandler;
            Loaded += new RoutedEventHandler(Window1_Loaded);
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            handlerView.CameraHandlerViewModel = new CameraHandlerViewModel(_cameraHandler);
        }

        private void closeCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            _cameraHandler.WantExit = true;
            Thread.Sleep(500);
            Application.Current.Shutdown();
        }

        private void mnuStartWebcam_Click(object sender, RoutedEventArgs e)
        {
            var results = DeviceInfoFactory.GetAvailableCaptureDevices(null);
            cameraControl.SetVideoDevice(results.Count == 1 ? results[0] : results[1]);
            cameraControl.StartupWebCam();
        }

        private void mnuStartBroadcast_Click(object sender, RoutedEventArgs e)
        {
            if (_broadcaster == null)
            {
                _server = new Communications();
                _broadcaster = new Broadcaster(_server, _cameraHandler);
                _broadcaster.StartBroadcasting();
            }
        }
    }
}
